import java.util.LinkedList;

public class MySingleList implements ISingleList{

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void creatList(){
        ListNode node = new ListNode(0);
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);

        node.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node;
    }

    @Override
    public void addFirst(int data) {
        ListNode add = new ListNode(data);
        if(this.head == null){
            this.head = add;
            return;
        }
        add.next = this.head;
        this.head = add;
    }

    @Override
    public void addLast(int data) {
        ListNode add = new ListNode(data);
        if(this.head == null){
            this.head = add;
            return;
        }
        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = add;
    }


    public boolean checkPos(int index){
        if(index < 0 || index > size()){
            return  false;
        }
        return true;
    }

    @Override
    public void addIndex(int index, int data) {
        if(!checkPos(index)){
            return;
        }

        //creatList();
        ListNode node = new ListNode(data);
        //头插
        if(index == 0){
           /* node.next = head;
            head = node;*/
            addFirst(data);
        }else {
            //中间插入、尾插
            ListNode cur = this.head;
            while(index > 1){
                cur = cur.next;
                index--;
            }
            node.next = cur.next;
            cur.next = node;
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //creatList();
        ListNode cur = head;
        if(cur.val == key){
            head = cur.next;
        }else {
            while (cur != null){
                if(cur.next.val == key){
                    ListNode curN = cur.next;
                    cur.next = curN.next;
                    return;
                }
                cur = cur.next;
            }
            System.out.println("链表中没有key!");
        }
    }

    @Override
    public void removeAllKey(int key) {
        if (head == null){
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null){
            if (cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key){
            head = head.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur = null;
            cur = curN;
        }
        head = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public ListNode myReverseList() {
        if(head == null){
            return head;
        }
        ListNode cur = head;
        ListNode curN = head.next;
        ListNode pos = curN.next;
        while(curN != null){
            curN.next = cur;
            cur = curN;
            curN = pos;
            if(pos != null){
                pos = pos.next;
            }else{
                break;
            }
        }
        head.next = null;
        head = cur;
        return head;
    }

    public ListNode reverseList() {
        if(head == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }

    public ListNode middleNode() {
        if(head == null){
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode FindKthToTail (int k) {
        // write code here
        if(head == null){
            return head;
        }
        if(k < 0){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        int tmp = 0;
        while(tmp!= k-1){
            fast = fast.next;
            if(fast == null){
                return null;
            }
            tmp++;
        }

        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


    public ListNode partition(int x) {
        ListNode bf = null;
        ListNode be = null;
        ListNode af = null;
        ListNode ae = null;
        ListNode cur = head;

        while (cur != null){
            if (cur.val < x){
                if (bf == null){
                    bf = be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if (af == null){
                    af = ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }

        if (bf == null){
            return af;
        }
        if (af != null){
            ae.next = null;
        }
        be.next = af;
        return bf;
    }

    public boolean chkPalindrome(ListNode A) {
        if(head == null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        //找到中间位置
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        //中间开始逆序
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //开始判断回文
        ListNode tmp = head;
        while (tmp != slow) {
            if (tmp.val != slow.val) {
                return false;
            }
            if(tmp.next == slow){
                return true;
            }
            tmp = tmp.next;
            slow = slow.next;
        }
        return true;
    }

    public ListNode detectCycle() {
        if(head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return null;
        }

        slow = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA;
        ListNode ps = headB;
        int len1 = 0;
        int len2 = 0;
        while(pl != null){
            len1++;
            pl = pl.next;
        }
        while(ps != null){
            len2++;
            ps = ps.next;
        }
        pl = headA;
        ps = headB;
        int len = len1 - len2;
        if(len < 0 ){
            pl = headB;
            ps = headA;
            len = len2 - len1;
        }
        while(len != 0){
            pl = pl.next;
            len--;
        }
        while(pl != ps){
            pl = pl.next;
            ps = ps.next;
        }
        return pl;
    }
}

